package cn.yoyo.controller;

import cn.yoyo.entity.User;
import cn.yoyo.service.*;
import cn.yoyo.tools.RespJson;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Map;

@Controller
@RequestMapping("/api/public")
public class PublicController {
    @Autowired
    UserService userService;
    @Autowired
    LargefileService largefileService;
    @Autowired
    BlogService blogService;
    @Autowired
    BlogTypeService blogTypeService;
    @Autowired
    ContentService contentService;

    @RequestMapping("/user/go2Add")
    public String go2Add(){
        return "user/add";
    }

    @RequestMapping("/user/go2Login")
    public String go2Login(){
        return "user/login";
    }

    @RequestMapping("/user/add")
    @ResponseBody
    public Map<String,Object> add(User user, @RequestParam(value = "img",required = false)MultipartFile file)throws Exception{
        userService.add(user,file);
        return RespJson.pack(200,"添加成功",userService.findAll());
    }

    @RequestMapping("/blog/findById/{blogId}")
    @ResponseBody
    public Map<String,Object> findById(@PathVariable("blogId")String blogId){
        return RespJson.pack(200,"",blogService.findById(blogId));
    }

    @RequestMapping("/blog/findByType/{typeId}")
    @ResponseBody
    public Map<String,Object> findByType(@PathVariable("typeId")String typeId){
        return RespJson.pack(200,"",blogService.findByType(typeId));
    }

    @RequestMapping("/content/findById/{blogId}/{contentId}")
    @ResponseBody
    public Map<String,Object> findContentById(@PathVariable("blogId")String blogId,@PathVariable("contentId")String contentId){
        return RespJson.pack(200,"",contentService.findById(blogId,contentId));
    }

    @RequestMapping("/blog/go2ReadAll")
    public String go2ReadAll(){
        return "blog/blog";
    }

    @RequestMapping("/blog/findAll")
    @ResponseBody
    public Map<String,Object> findAllBlog(){
        return RespJson.pack(200,"",blogService.findAllCanSee());
    }

    @RequestMapping("/blogType/findAll")
    @ResponseBody
    public Map<String,Object> findAllBlogType(){
        return RespJson.pack(200,"",blogTypeService.findAll());
    }

    @RequestMapping("/show/{id}")
    public void show(@PathVariable("id") String id, HttpServletResponse response){
        System.out.println("id="+id);
        try {
            InputStream inputStream = largefileService.getInpuStreamById(id);
            if (inputStream!=null){
                response.setContentType("image/jpg");
                IOUtils.copy(inputStream,response.getOutputStream());
            }
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("NOT FOUND IMG ID="+id);
        }
    }

}
